#ifndef __HTTPUTILS__
#define __HTTPUTILS__

#include <e32base.h>
#include <http\RHTTPTransaction.h>
#include <http\RHTTPSession.h>
#include <http\MHTTPTransactionCallback.h>
#include <http\MHTTPDataSupplier.h>
#include "Function.h"
#include <ezgzip.h>



RHTTPTransaction RHTTPSessionOpenTransactionL(RHTTPSession& aSession, const TDesC8& aUrl, MHTTPTransactionCallback& aCallback, TInt aMethod);
void RHTTPRequestSetCookie(RHTTPSession& aSession, RHTTPRequest aRequest, const TDesC8& aCookieName, const TDesC8& aCookieValue);
void RHTTPRequestSetHeaderL(RHTTPSession& aSession, RHTTPRequest aRequest, TInt aHdrField, const TDesC8& aHdrValue);
void RHTTPSessionSetHeaderL(RHTTPSession& aSession, TInt aHdrField, const TDesC8& aHdrValue);
HBufC8* RHTTPRequestGetCookie(RHTTPSession& aSession, RHTTPResponse aResponse, const TDesC8& aCookieName);



struct CDeflateDecompressor : public CBase, public MEZBufferManager
	{
public:
	CDeflateDecompressor();
	static CDeflateDecompressor* NewLC();
	static CDeflateDecompressor* NewL();
	~CDeflateDecompressor();

	const TDesC8& DecompressGZipL(const TDesC8& aGzipBuffer);
	const TDesC8& DecompressGZip2L(const TDesC8& aGzipBuffer);

private:
	CEZDecompressor* iDecompressor;
	TPtrC8 iInput;
	RBuf8 iOutputBuf;
	TPtr8 iOutput;

	void ConstructL();
	void InitializeL(CEZZStream &aZStream);
	void NeedInputL(CEZZStream &aZStream);
	void NeedOutputL(CEZZStream &aZStream);
	void FinalizeL(CEZZStream &aZStream);
	};




class CHTTPTransactionTarget : public CBase, public MHTTPTransactionCallback
	{
private:
	RBuf8 iBody;
	TInt iLastError;
	scalp::CFunction<void(RHTTPTransaction, THTTPEvent, TInt, TPtrC8)>* iRun;
	scalp::CFunction<TInt(TInt, RHTTPTransaction, THTTPEvent)>* iRunError;
	scalp::CFunction<void(RHTTPTransaction, THTTPEvent, TInt, TPtrC8, TInt)>* iRunBodyData;
	CHTTPTransactionTarget();
	void ConstructL();
public:
	static CHTTPTransactionTarget* NewLC();
	static CHTTPTransactionTarget* NewL();
	const TDesC8& Body() { return iBody; }
	void SetRun(scalp::CFunction<void(RHTTPTransaction, THTTPEvent, TInt, TPtrC8)>* aRun);
	void SetRunBodyData(scalp::CFunction<void(RHTTPTransaction, THTTPEvent, TInt, TPtrC8, TInt)>* aRunBodyData);
	void SetRunError(scalp::CFunction<TInt(TInt, RHTTPTransaction, THTTPEvent)>* aRunError);
    virtual void MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent);
    virtual TInt MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& aEvent);
	virtual ~CHTTPTransactionTarget();
	};


class CHTTPTransactionSource : public CBase, public MHTTPDataSupplier
	{
private:
	HBufC8* iPostData;
public:
	void SetPostDataL(const TDesC8& aPostData);
	virtual TBool GetNextDataPart(TPtrC8& aDataPart);
	virtual void ReleaseData();
	virtual TInt OverallDataSize();
	virtual TInt Reset();
	virtual ~CHTTPTransactionSource();
	};

#endif /* __HTTPUTILS__ */
